/*
 * IFileLogger.cpp
 *
 *  Created on: 26.08.2012
 *      Author: caffeine
 */

#include "IFileLogger.h"
#include <StringUtils.h>


namespace irr {
namespace log {

int protocol_version=1;

void IFileLogger::LOG_FUNC(irr::thread::ThreadID current_thread,char*text,irr::ELOG_LEVEL log,const char*filename,const char*function,irr::u32 line,void*this_pointer){
	IFileLogger*self=(IFileLogger*)this_pointer;
	self->grab();
	self->add(current_thread,text,log,filename,function,line);
	self->drop();
}

IFileLogger::IFileLogger(irr::io::IFileSystem*fs) {
	fileSystem=fs;
	fileSystem->grab();
	fileName="run-log";
	ext=".log";
	usinThreadIDINFileName=false;
	binaryFormat=false;
}

void IFileLogger::add(irr::thread::ThreadID current_thread,char*text,irr::ELOG_LEVEL log,const char*filename,const char*function,irr::u32 line){
	irr::io::IWriteFile*writer=0;

	writer=fileSystem->createAndWriteFile(fileName+ext,fileSystem->existFile(fileName+ext));
	if (!writer){
		printf("Can't create log file %s",(fileName+ext).c_str());
		exit(0);
	}
	irr::u32 text_length=strlen(text);
	irr::u32 current_thread_id=irr::thread::getCurrentID();


	irr::u32 filename_length=strlen(filename);
	irr::u32 function_length=strlen(function);
	irr::u32 line_pp=line;
	irr::u8 log_id;
	if (log==irr::ELL_WARNING)
		log_id=1;
	else
		if (log==irr::ELL_ERROR)
			log_id=2;
		else
			if (log==irr::ELL_NONE)
				log_id=3;
			else
				log_id=0;
	if (protocol_version==1){
		if (binaryFormat){
			writer->write(&current_thread_id,sizeof(irr::u32));

			writer->write(&filename_length,sizeof(irr::u32));
			writer->write(&filename[0],filename_length);

			writer->write(&function_length,sizeof(irr::u32));
			writer->write(&function[0],function_length);

			writer->write(&line_pp,sizeof(irr::u32));

			writer->write(&log_id,sizeof(irr::u8));

			writer->write(&text_length,sizeof(irr::u32));
			writer->write(&text[0],text_length);
		}else{
			char end_line='\n';
			char tab_line='\t';
			irr::core::stringc temp;
			temp=irr::core::stringc((irr::u32)irr::thread::getCurrentID());
			writer->write(temp.c_str(),temp.size());
			writer->write(&tab_line,1);
			temp=filename;
			writer->write(temp.c_str(),temp.size());
			writer->write(&tab_line,1);
			temp=function;
			writer->write(temp.c_str(),temp.size());
			writer->write(&tab_line,1);
			temp=irr::core::stringc(line_pp);
			writer->write(temp.c_str(),temp.size());
			writer->write(&tab_line,1);
			temp=irr::core::stringc(log);
			writer->write(temp.c_str(),temp.size());
			writer->write(&tab_line,1);
			temp=irr::core::stringc(text);
			writer->write(temp.c_str(),temp.size());
			writer->write(&tab_line,1);
			writer->write(&end_line,1);

		}
	}
	writer->drop();
}

irr::u32 IFileLogger::getProtocolVersion(){
	return protocol_version;
}

void IFileLogger::setThisAsDefaultLogger(){
	irr::log::Log::setReceiver(LOG_FUNC,this);
}

IFileLogger::~IFileLogger() {
	fileSystem->drop();
}

void IFileLogger::setUsinThreadIDINFileName(bool a){
	usinThreadIDINFileName=a;
}

bool IFileLogger::isUsinThreadIDINFileName(){
	return usinThreadIDINFileName;
}

} /* namespace log */
} /* namespace irr */
